package com.idopartx.phonelightning.utils

import android.content.Context
import android.database.ContentObserver
import android.hardware.Camera
import android.hardware.camera2.CameraManager
import android.net.Uri
import android.os.Build
import android.os.Handler
import com.idopartx.phonelightning.common.Constant
import com.idopartx.phonelightning.entity.NotifyLightningCache
import com.tencent.mmkv.MMKV

class SmsObserver(var applicationContext: Context, handler: Handler?) : ContentObserver(handler) {

    // 监听器对象
    private var camera: Camera? = null
    private var isShowing: Boolean = false

    override fun onChange(selfChange: Boolean, uri: Uri?) {
        super.onChange(selfChange)
        if (uri.toString() == "content://sms/raw") {

            val cache = MMKV.defaultMMKV().decodeParcelable(
                Constant.MMKV_SAVE_SMS_LIGHTNING,
                NotifyLightningCache::class.java
            )

            if (cache != null) {
                openStar(cache)
            }
            return
        }
    }


    /**
     * 开启闪烁
     */
    private fun openStar(notifyLightningCache: NotifyLightningCache) {
        isShowing = true
        Thread {
            for (i in 0 until (notifyLightningCache.showTimes ?: 2)) {
                notifyLightningCache.lightNightList?.forEach {
                    open()
                    try {
                        Thread.sleep(it.light.toLong())
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
                    close()
                    try {
                        Thread.sleep(it.night.toLong())
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
                }
                if (i == notifyLightningCache.showTimes) {
                    isShowing = false
                }
            }
        }.start()
    }

    /**
     * 打开闪光灯
     *
     * @return
     */
    private fun open() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                val camManager =
                    applicationContext.getSystemService(Context.CAMERA_SERVICE) as CameraManager

                val cameraId = camManager.cameraIdList[0]

                camManager.setTorchMode(cameraId, true)
            } else {
                camera = Camera.open()
                camera?.startPreview()
                val parameters = camera?.parameters
                parameters?.flashMode = Camera.Parameters.FLASH_MODE_TORCH
                camera?.parameters = parameters
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 关闭闪光灯
     *
     * @return
     */
    private fun close() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                val camManager =
                    applicationContext.getSystemService(Context.CAMERA_SERVICE) as CameraManager

                val cameraId = camManager.cameraIdList[0]

                camManager.setTorchMode(cameraId, false)
            } else {
                val parameters = camera?.parameters
                parameters?.flashMode = Camera.Parameters.FLASH_MODE_OFF
                camera?.parameters = parameters
                camera?.release()
                camera = null
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}